- 加入Pausable(暫停功能)
暫停功能讓合約擁有者能夠在某些情況下暫停代幣交易,可以使用一個paused狀態變量來控制是否允許代幣交易,並在transfer和其他函數中加入檢查。
bool public paused = false;
modifier whenNotPaused() {
require(!paused, "Token transfers are paused");
_;
}
function pause() public {
require(msg.sender == owner, "Only the owner can pause");
paused = true;
}
function unpause() public {
require(msg.sender == owner, "Only the owner can unpause");
paused = false;
}
function transfer(address _to, uint256 _value) public whenNotPaused returns (bool success) {
require(balanceOf[msg.sender] >= _value, "Insufficient balance");
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
- 實現Owner更換功能
允許合約擁有者將擁有權轉讓給其他人。
address public owner;
modifier onlyOwner() {
require(msg.sender == owner, "Caller is not the owner");
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0), "New owner is the zero address");
owner = newOwner;
}